package com.pachong.android.baseuicomponent.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.pachong.android.baseuicomponent.IComponentStatable;
import com.pachong.android.baseuicomponent.view.CenterToolbar;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;

/**
 * 1. Activity定义了许多的状态，用户可以通过set(state)来切换状态，每个状态对应一个View，用户还可以自定义这个View
 * <p/>
 * 每个状态对应一个UI 布局,用户可以自定义这些布局,通过setXX方法（e.g setEmptyView）
 * <p>
 * 2. Fragment默认没有title bar,User可以自定义自己的title bar
 *
 * @author Vivi Peng
 * @version 1.0.0
 * @e-mail pengveivei@sina.cn
 * @QQ 897058316
 * @since 1979-01-01
 */
public abstract class BaseFragment extends StateFragment implements IComponentStatable {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        setCustomToolbar(new CenterToolbar(getActivity()));
        getCustomToolbar().setVisibility(View.GONE);
        ButterKnife.bind(this, view);
        return view;

    }


    public void setTitle(CharSequence title) {
        getCustomToolbar().setTitle(title);
    }

    public void setTitle(int titleId) {
        getCustomToolbar().setTitle(titleId);
    }

    @Override
    public CenterToolbar getCustomToolbar() {
        return (CenterToolbar) super.getCustomToolbar();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getSimpleName());
        MobclickAgent.onResume(getActivity());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getSimpleName());
        MobclickAgent.onPause(getActivity());
    }


    /**
     * 设置刷新的UI是否可以取消
     *
     * @param cancelable
     */
    public void setRefreshingCancelable(final boolean cancelable) {
        getEmptyRefreshingView().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (cancelable) {
                    return false;
                } else {
                    return true;
                }
            }
        });
    }


    protected <V extends View> V findView(int resId) {
        return (V) getDataView().findViewById(resId);
    }


}
